﻿using System;
using System.Configuration;
using mojoPortal.Net;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using ud_gSDMS.Code_Service;
using mojoPortal.Business.WebHelpers;
using System.Linq;
using System.Data;
using Telerik.Web.UI;

namespace ud_gSDMS.Testing
{
    public partial class SendProject : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private SiteUser _siteUser;

        void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Send Require";
            //if (!UserCanEditPage() || !UserCanViewPage())
            //{
            //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            //}


            //SiteUser.ConfirmRegistration()
            if (_siteUser == null)
            {
                WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
            }
            else
            {
                if (gSDMS_BL.mp_User_IsRole(_siteUser.UserId, "13|1") == false)
                {
                    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                }
            }

            //init database
        }

        #region event
        void btnSend_Click(object sender, EventArgs e)
        {
            ud_gSDMS_PROJECT_TEST temp = null;

            try
            {
                temp = gSDMS_BL.ud_gSDMS_PROJECT_TESTs_Upd(
                    null,
                    int.Parse(this.radComBBProjectCode.SelectedValue),
                    this.datCreateDate.SelectedDate,
                    int.Parse(this.radComBBSender.SelectedValue),
                    int.Parse(this.radComBBAssignTo.SelectedValue),
                    this.datStartDate.SelectedDate,
                    this.datEndDate.SelectedDate,
                    this.txtNotes.Text,
                    1,
                    true
                    );
            }
            catch (Exception ex)
            {

            }

            var assigned = gSDMS_BL.mp_User_get(radComBBSender.SelectedValue, null);
            var userManager = gSDMS_BL.mp_User_get(radComBBAssignTo.SelectedValue, null);
            var cc = gSDMS_BL.ud_gSDMS_PROJECT_Get(Convert.ToInt32(radComBBProjectCode.SelectedValue));

            var emailFrom = userManager.Email;
            var emailCc = cc.mp_User.Email;
            var emailTo = assigned.Email;

            var subject = string.Format("[gSDMS-TESTING] PLEASE ACCEPT A NEW TEST PROJECT: {0} FROM gSDMS", radComBBProjectCode.Text);
            var textBody = "Dear " + assigned.LoginName + "<br/><br/> We have created a project test : <b>" + radComBBProjectCode.Text +
                "</b><br/><br/> Please enter follow link to check :" + SiteRoot + "/gsdmsmodule/Testing/TManage.aspx" + " <br/><br/> <b><u>Discription</b></u> : " + txtNotes.Text + "<br/><br/> Regard, <br/>" + userManager.LoginName;
            var siteGuid = siteSettings.SiteGuid;

            Library.SendMail(emailFrom, emailTo, emailCc, string.Empty, subject, textBody, siteGuid);
            

            WebUtils.SetupRedirect(this, SiteRoot + "/gsdmsmodule/testing/TManage.aspx");

        }

        #endregion event

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        private void LoadData()
        {
            //Load Project code and add to comBBProjectCode

            if (_siteUser != null)
            {
                this.radComBBProjectCode.DataValueField = "Project_ID";
                this.radComBBProjectCode.DataTextField = "Code";
                this.radComBBProjectCode.DataSource = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Not_Test_FromUser(_siteUser.UserId);
                this.radComBBProjectCode.DataBind();

                //Load Create date
                this.datCreateDate.SelectedDate = DateTime.Today;

                //Load Sender
                var sender = gSDMS_BL.mp_User_Lst("13");
                this.radComBBSender.DataValueField = "UserID";
                this.radComBBSender.DataTextField = "LoginName";
                this.radComBBSender.DataSource = sender;
                this.radComBBSender.DataBind();
                this.radComBBSender.SelectedValue = _siteUser.UserId.ToString();



                //Load Assign To
                var assign = gSDMS_BL.mp_User_Lst("3");
                this.radComBBAssignTo.DataValueField = "UserID";
                this.radComBBAssignTo.DataTextField = "LoginName";
                this.radComBBAssignTo.DataSource = assign;
                this.radComBBAssignTo.DataBind();
            }

        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);


            

            datEndDate.SelectedDateChanged += new Telerik.Web.UI.Calendar.SelectedDateChangedEventHandler(RDTPSelectedDateChanged);
            datStartDate.SelectedDateChanged += new Telerik.Web.UI.Calendar.SelectedDateChangedEventHandler(RDTPSelectedDateChanged);
            SuppressPageMenu();
            if (SiteUtils.GetCurrentSiteUser() != null)
            {
                _siteUser = SiteUtils.GetCurrentSiteUser();
                btnSend.Click += btnSend_Click;
                LoadData();
            }
        }

        private void RDTPSelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}